﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace Northwind.NET.FE.Reports
{
    public partial class EmployeeSalesByCountryReport : Form
    {
        private DateTime _fromDate;
        private DateTime _toDate;

        public EmployeeSalesByCountryReport()
        {
            InitializeComponent();
        }

        private void EmployeeSalesByCountryReport_Load(object sender, EventArgs e)
        {
            _fromDate = this.fromDateTimePicker.Value;
            _toDate = this.toDateTimePicker.Value;
            this.reportViewer1.LocalReport.EnableExternalImages = true;
            RefreshReport();

            this.WindowState = FormWindowState.Maximized;  
        }

        private void RefreshReport()
        {
            this.employeeSalesByCountryReportTableAdapter.FillByShipped(this.simpleReportsDataSet.EmployeeSalesByCountryReport, _fromDate, _toDate);

            // Create two parameters for display of the selected date range.
            ReportParameter[] parameters = new ReportParameter[2];
            ReportParameter reportParameterDateFrom = new ReportParameter("DateFrom", _fromDate.ToString(), false);
            ReportParameter reportParameterDateTo = new ReportParameter("DateTo", _toDate.ToString(), false);
            parameters[0] = reportParameterDateFrom;
            parameters[1] = reportParameterDateTo;
            // Add the two parameters to the report.
            reportViewer1.LocalReport.SetParameters(parameters);

            this.reportViewer1.RefreshReport();
        }

        private void fromDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker dateTimePicker = (DateTimePicker)sender;
            if (dateTimePicker.Focused)
            {
                if (dateTimePicker.Value > this.toDateTimePicker.Value)
                {
                    toDateTimePicker.Value = dateTimePicker.Value;
                    _toDate = this.toDateTimePicker.Value;
                }
                _fromDate = this.fromDateTimePicker.Value;
                this.refreshButton.Enabled = true;
            }
        }

        private void toDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker dateTimePicker = (DateTimePicker)sender;
            if (dateTimePicker.Focused)
            {
                if (dateTimePicker.Value < this.fromDateTimePicker.Value)
                {
                    fromDateTimePicker.Value = dateTimePicker.Value;
                    _fromDate = this.fromDateTimePicker.Value;
                }
                _toDate = this.toDateTimePicker.Value;
                this.refreshButton.Enabled = true;
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            this.refreshButton.Enabled = false;
            RefreshReport();
        }
    }
}
